package com.zh.baselibrary.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/10/18.
 */
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseRecyclerAdapter.BaseViewHolder> {

    protected List<T> mData;//数据集合
    protected Context mContext;//上下文
    protected OnItemClickListener onItemClickListener;//单击事件
    protected OnItemLongClickListener onItemLongClickListener;//长按事件
    protected OnItemViewClickListener onItemViewClickListener;//view单机事件

    public BaseRecyclerAdapter(List<T> mData) {
        this.mData = mData;
        setHasStableIds(true);
    }

    @Override
    public BaseRecyclerAdapter.BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(getLayoutId(viewType), parent, false);
        final BaseViewHolder baseViewHolder = new BaseViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null)
                    onItemClickListener.onItemClick(v, baseViewHolder.getLayoutPosition());
            }
        });
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemLongClickListener != null)
                    onItemLongClickListener.onItemLongClick(v, baseViewHolder.getLayoutPosition());
                return false;
            }
        });
        return baseViewHolder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerAdapter.BaseViewHolder holder, final int position) {
        T t =null;
        if(position<mData.size())
        t = mData.get(position);
        bindData(holder, position, t);
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    /**
     * 刷新数据
     */
    public void refresh() {
        notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param data
     */
    public void addData(List<T> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void clearAdd(List<T> data){
        int preSize = this.mData.size();
        if(preSize > 0) {
            this.mData.clear();
            notifyItemRangeRemoved(0, preSize);
        }
        if(data.size()>0) {
            this.mData.addAll(data);
            notifyItemRangeChanged(0, data.size());
        }
    }
    public void clear(){
        this.mData.clear();
        notifyDataSetChanged();
    }

    /**
     * 添加数据(局部刷新，局部刷新时必须充血getItemId方法，同时setHasStableIds(true))
     *
     * @param data
     */
    public void addDataWithoutAnim(List<T> data) {
        if (data == null)
            return;
        int size = mData.size();
        this.mData.addAll(data);
        notifyItemRangeChanged(size, data.size());
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 初始化item布局，获取item
     *
     * @return
     */
    protected abstract int getLayoutId(int viewType);

    /**
     * 绑定数据
     *
     * @param holder   具体的viewHolder
     * @param position 对应的索引
     * @param t
     */
    protected abstract void bindData(BaseViewHolder holder, int position, T t);

    /**
     * 设置文本属性
     *
     * @param view
     * @param text
     */
    public void setItemText(View view, String text) {
        if (view instanceof TextView) {
            ((TextView) view).setText(text);
        }
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }


    public void setOnItemViewClickListener(OnItemViewClickListener onItemViewClickListener) {
        this.onItemViewClickListener = onItemViewClickListener;
    }

    public interface OnItemViewClickListener {
        void onItemViewClick(View v, int position);
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(View v, int position);
    }

    /**
     * 封装ViewHolder ,子类可以直接使用
     */
    public class BaseViewHolder extends RecyclerView.ViewHolder {

        private Map<Integer, View> mViewMap;

        public BaseViewHolder(View itemView) {
            super(itemView);
            mViewMap = new HashMap<>();
        }

        /**
         * 获取设置的view
         *
         * @param id
         * @return
         */
        public View getView(int id) {
            View view = mViewMap.get(id);
            if (view == null) {
                view = itemView.findViewById(id);
                mViewMap.put(id, view);
            }
            return view;
        }
    }
}
